library(tidyverse); library(readxl); library(glue); library(zoo)
library(lmtest); library(sandwich);
library(huxtable); library(broom)
library(lubridate); library(data.table)
library(writexl)
library(qs)
library(ggthemes); library(scales); library(ggrepel)
library(gridExtra)
library(textstem)

source('codes/fn_predict_lm.R')

select = dplyr::select
filter = dplyr::filter



#------------------------------------------------------------------------
# Function to detrend by AR(h)
#------------------------------------------------------------------------

fn_resid = function(x, h) {
  rhs = paste0('lag(x, ', 1:h, ')', collapse = ' + ') 
  fml = glue('x ~ {rhs}') |> as.formula()
  mod = lm(fml)
  e = resid(mod) |> unname()
  return(c(rep(NA, h), e))
}



#------------------------------------------------------------------------
# Function to create PLS indexes
#------------------------------------------------------------------------

fn_create_pls = function(dt, yvar = 'mkt1', topic_list, freq, pls_name) {
  
  pls = fn_construct_pls_full(
    df    = dt,
    xvar  = topic_list,
    yvar  = yvar,
    scale = FALSE
  )$x_pls
  
  names(pls)[2] = pls_name
  
  dt = dt %>% 
    left_join(pls, by = freq)
  
  return(dt)
}



fn_create_pls_recursive = function(dt, yvar = 'mkt1', topic_list, freq, pls_name) {
  
  pls = fn_construct_pls_recursive(
    df        = dt,
    xvar      = topic_list,
    yvar      = yvar, 
    ini_win   = 120, 
    fixed_win = FALSE
  )$x_pls
  
  names(pls)[2] = paste0(pls_name, '_recursive')
  
  dt = dt %>% 
    left_join(pls, by = freq)
  
  return(dt)
}
